//
//  FSFarmerVC.m
//  FieldStory
//
//  Created by itte on 2017/6/22.
//  Copyright © 2017年 funmain. All rights reserved.
//

#import "FSFarmerVC.h"
#import "FamerCell.h"

@interface FSFarmerVC ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *lbProductName;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *dataArray;
@end

@implementation FSFarmerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"认养家畜";
    self.dataArray = [NSMutableArray array];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"FamerCell" bundle:nil] forCellReuseIdentifier:@"FamerCellID"];
    [self getFarmerList];
}

-(void)getFarmerList
{
     WeakSelfType blockSelf = self;
    [[AFNetworkManager sharedInstance] httpGetWithUrl:@"farms" params:nil showHUD:NO toShowHUDView:nil success:^(id responseObject, NSString *errorMessage) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            NSArray *data = [FSFarmerModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            [blockSelf.dataArray removeAllObjects];
            [blockSelf.dataArray addObjectsFromArray:data];
            [blockSelf.tableView reloadData];
        }
    } fail:^(NSString *errorCode, NSString *errorMessage) {
        
    }];
}

-(void)btnChoiseClick:(UIButton *)btn
{
    NSLog(@"%ld",btn.tag);
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FamerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FamerCellID"];
    if (cell == nil) {
        cell = [[FamerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FamerCellID"];
    }
    
    FSFarmerModel *farmer = self.dataArray[indexPath.section];
    cell.lbName.text = farmer.name;
    [cell.imgFarmer sd_setImageWithURL:[NSURL URLWithString:farmer.images] placeholderImage:[UIImage imageNamed:@"icon_wx"]];
    cell.lbAddr.text = farmer.province;
    cell.btnChoise.tag = 1000+indexPath.section;
    [cell.btnChoise addTarget:self action:@selector(btnChoiseClick:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self pushViewController:@"FSFarmerDetailVC" withParams:nil];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90.f;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1.0f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 15.f;
}
@end
